//
//  FenLeiMainViewController.m
//  ShangChengMoBan
//
//  Created by 张铁军 on 16/8/13.
//  Copyright © 2016年 ping. All rights reserved.
//

#import "FenLeiMainViewController.h"
#import "ShangChengHeaderView.h"
#import "ChanPinCollectionViewCell.h"
#import "ShangHuFirstJi.h"
#import "erSanJiFenLeiModel.h"
#import "ProductListViewController.h"
#import "SCSearchViewController.h"
@interface FenLeiMainViewController ()

@end

@implementation FenLeiMainViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    _fenLeishuJuAry = [[NSMutableArray alloc]init];
    _productTypeAry = [[NSMutableArray alloc]init];
    [self drawNavWithTitle:@"商城"];
    [self drawView];
    [self requestData];
}

-(void)drawNavWithTitle:(NSString *)Title{
    
    UILabel *labNav = [[UILabel alloc]initWithFrame:CGRectMake(0, 0,KScreenWidth , 64)];
    labNav.backgroundColor = ZONGCOLOR;
    [self.view addSubview:labNav];
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(_kscW * 40,  30, 260 * _kscW, 24)];
    imageView.userInteractionEnabled = YES;
    imageView.layer.cornerRadius = 12;
    imageView.backgroundColor = [UIColor whiteColor];
    
    _textField = [[UITextField alloc]initWithFrame:CGRectMake(5, 2 ,250 * _kscW, 20)];
    UIImageView *souSuoImage = [[UIImageView alloc]initWithFrame:CGRectMake(0, _kscH * 2, 20, 20)];
    souSuoImage.image = [UIImage imageNamed:@"放大镜"];
    
    [_textField addTarget:self action:@selector(search:) forControlEvents:UIControlEventTouchDown];
    _textField.leftView = souSuoImage;
    _textField.leftViewMode = UITextFieldViewModeAlways;
    _textField.placeholder = @"请输入关键词搜索";
    _textField.font = [UIFont systemFontOfSize:14];
    
    [imageView addSubview:_textField];
    
    [self.view addSubview:imageView];
}

-(void)reloadeGuangGaoViewWith:(NSString *)shangHuId{
//    NSMutableDictionary *requestDic = [NSMutableDictionary dictionary];
//    [requestDic  setObject:[NSString stringWithFormat:@"fadealertype_%@",shangHuId] forKey:@"code"];
//    [TCUtils showLoadingWithView:self.view ];
//    [NetWorking jieKouQingQiu:ShouYeImage And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
//        
//        [TCUtils hiddonLoadingWithView:self.view];
//        if (!error) {
//            if([[dic objectForKey:Code]integerValue] ==200){
//                NSArray *resultArr = [NSArray arrayWithArray:[dic objectForKey:Response]];
//                NSMutableArray *signArr = [[NSMutableArray alloc] init];
//                _guangGaoAry = [[NSMutableArray alloc]init];
//                [resultArr enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
//                    GuangGaoModel *model = [GuangGaoModel objectFromDictionary:obj];
//                    [signArr addObject:model.image];
//                    [_guangGaoAry addObject:model];
//                }];
//                NSMutableArray *imageAry = [[NSMutableArray alloc]init];
//                
//                for (NSString *imageStr in signArr) {
//                    [imageAry addObject:[NSString stringWithFormat:@"%@%@",IPStr,imageStr]];
//                }
//                
//                if (_guangGaoAry.count == 0) {
//                    gunDongHeight = 0;
//                    _topScrollView.hidden = YES;
//                }else{
//                    _topScrollView.hidden = NO;
//                    _topScrollView.imageURLStringsGroup = imageAry;
//                    gunDongHeight = 100 * _kscH;
//                }
//                [_shangChengCollecTionView reloadData];
//            }
//        }else{
//            NSLog(@"error:%@",error);
//        }
//    }];
}

-(void)clickBack{
    [self.navigationController popToRootViewControllerAnimated:YES];
}

-(void)requestData{
     [TCUtils showLoadingWithView:self.view];
    [NetWorking NEWjieKouQingQiu:FL_category ANDIP:IP And:nil anBlock:^(NSDictionary *dic, NSError *error) {
        [TCUtils hiddonLoadingWithView:self.view];
        if (!error) {
            
             NSArray *data = [dic objectForKey:Response];
           
            [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                NSDictionary *dic = obj;
                ShangHuFirstJi *type = [ShangHuFirstJi objectFromDictionary:dic];
                [_fenLeishuJuAry addObject:type];
                type = nil;
            }];
             if(_fenLeishuJuAry.count > 0){
                [self requestProduct:((ShangHuFirstJi *)[_fenLeishuJuAry firstObject])._id];
                [_fenLeiTableView reloadData];
                
                [_fenLeiTableView selectRowAtIndexPath:[NSIndexPath indexPathForItem:0 inSection:0] animated:NO scrollPosition:UITableViewScrollPositionTop];
            }
          }
        else{
            NSLog(@"error-->%@",error);
        }
    }];
    }
-(void)requestProduct:(NSString *)typeid{
    

    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc] init];
    [requestDic setObject:typeid forKey:@"goodsTypeId"];
   
    [TCUtils showLoadingWithView:self.view];
    [NetWorking NEWjieKouQingQiu:FL_category ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
        [TCUtils hiddonLoadingWithView:self.view];
        if (!error) {
            // NSDictionary *dic1 = [dic objectForKey:RESULT];
            //  id obj = [dic1 objectForKey:Response];
            //            NSLog(@"取出来的二级商铺信息-->%@",dic);
            _productTypeAry = [[NSMutableArray alloc]init];
            if ([[dic objectForKey:Code]intValue] == 200) {
                NSArray *data = [dic objectForKey:Response];
                [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
                    erSanJiFenLeiModel * erjiModel = [erSanJiFenLeiModel objectFromDictionary:obj];
                    [_productTypeAry addObject:erjiModel];
                    
                }];
                
                [_shangChengCollecTionView reloadData];
            }
        }
        else{
            NSLog(@"error-->%@",error);
        }
    }];
    
    
//    [self reloadeGuangGaoViewWith:typeid];
    
}
-(void)drawView{
    //    _shangChengGGScrollview = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, KreenWidth, 130 * _kscH)];
    
    //分类tableview
    _fenLeiTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, 80 * _kscW, KScreenHeight - 114)];
    _fenLeiTableView.delegate = self;
    _fenLeiTableView.dataSource = self;
    _fenLeiTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_fenLeiTableView];
    _fenLeiTableView.backgroundColor = FenLeiBeiJingHuiSe;
    
    UICollectionViewFlowLayout *flow1 = [[UICollectionViewFlowLayout alloc]init];
    //    flow.itemSize = CGSizeMake(KScreenWidth/2, 240);
    flow1.minimumInteritemSpacing = 0;
    flow1.minimumLineSpacing = 1;
    
    
//    //顶部滚动图
//    _topScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0 * _kscW,0, KScreenWidth - 80 * _kscW, 100 * _kscH)imageURLStringsGroup:nil];
//    
//    //
//    _topScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
//    _topScrollView.delegate = self;
//    //    cycleScrollView.titlesGroup = titles;
//    _topScrollView.hidden = YES;
//    _topScrollView.autoScrollTimeInterval = 1000000000000000000000000.0;
//    _topScrollView.dotColor = [UIColor yellowColor]; // 自定义分页控件小圆标颜色
//    _topScrollView.placeholderImage = [UIImage imageNamed:PLaceHolderImage];
   
    
    _shangChengCollecTionView = [[UICollectionView alloc]initWithFrame:CGRectMake(80 * _kscW, 64 , KScreenWidth - 80 * _kscW, KScreenHeight - 64 - 50 ) collectionViewLayout:flow1];
    _shangChengCollecTionView.delegate = self;
    _shangChengCollecTionView.dataSource = self;
    
    //    [_shangChengCollecTionView addSubview:_shangChengGGScrollview];
    
    _shangChengCollecTionView.backgroundColor = [UIColor whiteColor];
    
    [_shangChengCollecTionView registerClass:[ShangChengHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ShangChengHeaderView"];
    [_shangChengCollecTionView registerClass:[ShangChengHeaderView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ShangChengHeaderView1"];
    //
    [_shangChengCollecTionView registerClass:[ChanPinCollectionViewCell class] forCellWithReuseIdentifier:@"ChanPinCollectionViewCell"];
   //    [_shangChengCollecTionView addSubview:_topScrollView];
     [self.view addSubview:_shangChengCollecTionView];
 }

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return _productTypeAry.count;
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    
    erSanJiFenLeiModel *shanErjiType = [_productTypeAry objectAtIndex:section];
    return shanErjiType.goodsTypeList2.count;
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    ChanPinCollectionViewCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"ChanPinCollectionViewCell" forIndexPath:indexPath];
    erSanJiFenLeiModel *shanErjiType = [_productTypeAry objectAtIndex:indexPath.section];
    //3级数据
    ShangHuFirstJi *produtcType = [shanErjiType.goodsTypeList2 objectAtIndex:indexPath.item];
    [cell.myImageView sd_setImageWithURL:[NSURL URLWithString:[self requestImageStr]]];
    
    cell.titleLabel.text = produtcType.name;
//
    
    return cell;
}

- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    
    return CGSizeMake((KScreenWidth - 80 * _kscW)/ 3, 120 * _kscH);
}


//设置整个分区相对上下左右的间距

- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section

{
    //    if (section == 0) {
    //        return UIEdgeInsetsMake(120 * _kscH, 0, 0, 0);
    //    }
    return UIEdgeInsetsMake(0, 0, 0, 0);// top, left, bottom, right
    
}

//heardView 和footerView的视图
-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    ShangChengHeaderView *headView;
    
    if([kind isEqual:UICollectionElementKindSectionHeader])
    {
       
        headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"ShangChengHeaderView1" forIndexPath:indexPath];
                   
        
        headView.backgroundColor = FenLeiBeiJingHuiSe;
        //        headView = [collectionView registerClass:[ShangChengHeaderView class] forCellWithReuseIdentifier:@"ShangChengHeaderView"];
        
        erSanJiFenLeiModel *erJiModel = [_productTypeAry objectAtIndex:indexPath.section];
        
        
        headView.titleLael.text = erJiModel.name;
    }
    
    return headView;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    
    
    erSanJiFenLeiModel *erjiChanPin = [_productTypeAry objectAtIndex:indexPath.section];
    ShangHuFirstJi *produtcType = [erjiChanPin.goodsTypeList2 objectAtIndex:indexPath.item];

    ProductListViewController *scProductView = [[ProductListViewController alloc]init];
    scProductView.catId = [NSString stringWithFormat:@"%@",produtcType._id];
    scProductView.titLeName = produtcType.name;
    [self.navigationController pushViewController:scProductView animated:YES];
}

//heardView的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    
        return  CGSizeMake(KScreenWidth - 80 * _kscW, 30 * _kscH);
    
}


-(void)fenLeiBtnClicked:(UIButton *)sender{
    if (_shangChenghuiSeView.hidden == YES) {
        [UIView animateWithDuration:0.5 animations:^{
            _shangChenghuiSeView.hidden = NO;
            _fenLeiView.frame = CGRectMake(0 , _shangChenghuiSeView.top, KScreenWidth, 10 * _kscH);
        }];
    }else{
        [UIView animateWithDuration:0.5 animations:^{
            _shangChenghuiSeView.hidden = YES;
            _fenLeiView.frame = CGRectMake(0 - KScreenWidth, _shangChenghuiSeView.top, KScreenWidth, 10 * _kscH);
        }];
        
    }
}

#pragma tableViewdatasource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _fenLeishuJuAry.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"fenLei"];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"fenLei"];
        UILabel *fenGeLael = [[UILabel alloc]initWithFrame:CGRectMake(0, cell.height - 1, cell.width, 1)];
        fenGeLael.backgroundColor = LIGHTGRAYBACKCOLOR;
        [cell.contentView addSubview:fenGeLael];
    }
    
    cell.contentView.backgroundColor = [UIColor clearColor];
    UIView *aView = [[UIView alloc] initWithFrame:cell.contentView.frame];
    aView.backgroundColor = [UIColor whiteColor];
    cell.selectedBackgroundView = aView;
    
    ShangHuFirstJi *shanHuYiji = [_fenLeishuJuAry objectAtIndex:indexPath.row];
    cell.backgroundColor = FenLeiBeiJingHuiSe;
    cell.textLabel.text = shanHuYiji.name;
    [cell.textLabel setBackgroundColor:[UIColor clearColor]];
    cell.textLabel.font = [UIFont systemFontOfSize:12];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    ShangHuFirstJi *yiji = [_fenLeishuJuAry objectAtIndex:indexPath.row];
    
    [self requestProduct:yiji._id];
    
}




//更多btn
-(void)moreBtnClicked:(UIButton *)sender{
    
    
}

-(void)search:(id)sender{
    [_textField resignFirstResponder];
    SCSearchViewController *scSearchView = [[SCSearchViewController alloc]init];
    [self.navigationController pushViewController:scSearchView animated:YES];
    
}

#pragma mark 广告位点击
- (void)cycleScrollView:(SDCycleScrollView *)cycleScrollView didSelectItemAtIndex:(NSInteger)index{
//    BaseViewController *vc;
//    vc =[MyReturnViewController returnDetailViewController:[_guangGaoAry objectAtIndex:index]];
//    [self.navigationController pushViewController:vc animated:YES];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
